//
 

#import "NT_MenuTableVew.h"

 
@implementation NT_MenuTableVew

- (instancetype)init {
    if ([super init]) {
        _data = [[NT_CoreDataBase alloc]initWithCoreData:@"NoteCategory" modelName:@"NoteData" success:^{} fail:^(NSError * _Nullable error) {}];

        self.frame = [[UIScreen mainScreen] bounds];
        self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.6];
        [self addSubview:self.menuTableView];
        [self getDataWWWWWWW];
    }
    return self;
}

//#pragma mark ----------------- tableViewDelegate ----------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NoteCategory *model = _dataArray[indexPath.row];
    cell.textLabel.text = model.title;
    cell.imageView.image = (UIImage*)model.picture;
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self remove];
    NoteCategory *model = _dataArray[indexPath.row];
    if (self.dataBlock) {
        self.dataBlock(model.title, (UIImage*)model.picture);
    }
}
//
- (void)showV {
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window addSubview:self];
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        //        self.frame =
    } completion:^(BOOL finished) {
    }];
}

//
#pragma mark ----------------- 懒加载 ----------------
- (UITableView *)menuTableView {
    if (!_menuTableView) {
        _menuTableView = [[UITableView alloc]initWithFrame:CGRectMake(kRelativeWidth(20), UI_NAV_BAR_HEIGHT+kRelativeHeight(50), kRelativeWidth(120), kRelativeHeight(140)) style:UITableViewStylePlain];
        _menuTableView.dataSource = self;
        _menuTableView.delegate   = self;
        _menuTableView.tableFooterView = [UIView new];
        _menuTableView.rowHeight  = kRelativeHeight(35);
    }
    return _menuTableView;
}
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray new];
    }
    return _dataArray;
}

- (void)remove {
    [self removeFromSuperview];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self remove];
}
//
- (void)getDataWWWWWWW{
   //
    [_data ntReadEntity:nil ascending:NO filterStr:nil success:^(NSArray * _Nonnull results) {
        if (!results)return ;
        
        self.dataArray = [results copy];
        [self.menuTableView reloadData];
        
    } fail:^(NSError * _Nullable error) {
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
